/*
 * split.cpp
 *  - contains the split function for splitting a string into a vector of strings using
 *    spaces as the delimiter
 *
 *  Created on: May 1, 2014
 *      Author: Anthony Varghese
 */

#include <string>
#include <vector>
#include <algorithm> // for the for_each function
using std::string;
using std::vector;


vector<string> split(string e){
	vector<string> result;

	string t;
	std::for_each( e.begin(),
				   e.end(),
				   // the next argument to the for_each is a C++11 lambda
				   [&](char c){
						if (isspace(c)){
							// note that the lambda function can access t and result
							if (t.length() != 0 )
								result.push_back( t );
							t = "";
						} else {
							t = t + c;
						}
					});
	// there can be one last one token:
	if (t.length() != 0 )
		result.push_back( t );
	return result;
}

